$(function(){
    let opId = formatSearch(window.location.search)['opId'];
    $(`.nav > section a:eq(${opId-1})`).css({
        backgroundColor: "white",
        color: "rgb(104, 116, 112)"
    });
    $(`.aside-sec:eq(${opId - 1})`).addClass("aside-sec-show");
    if ( opId == 1 ){
        let page = 1 ; 
        let perPage = 10 ;
        getRenderData(page);
        function getRenderData(page){
            let uEle = document.querySelectorAll(".aside-sec .aside-body table tbody")[`${opId - 1}`];
            // console.log(page);
            ajax({
                url:'../../php/admin/allUsers.php',
                method:'post',
                dataType:'json',
                contentType:true,
                async:true,
                data:{
                    page,
                    perPage
                }
            }).then(res=>{
                renderUDom(uEle,res);
                getPageNum (perPage,page);
            });
        }
        function renderUDom(ele,data){
            ele.innerHTML = '' ;
            data.forEach((item,key)=>{
                let trEle = document.createElement('tr');
                let time = new Date(item['uTime']*1000);
                trEle.innerHTML = `<td>${key+1}</td>
                        <td>${item['userName']}</td>
                        <td>${item['passWord']}</td>
                        <td>${item['tel']}</td>
                        <td>${time.getFullYear()}/${time.getMonth()+1}/${time.getDate()}&nbsp;&nbsp;${time.getHours()}:${time.getMinutes()}</td>
                        <td><a class="uptBtn" myid=${item['id']}>修改</a></td>
                        <td><a class="delBtn" myid=${item['id']}>删除</a></td>`;
                let delBtnEle = trEle.querySelector(".delBtn");
                let uptBtnEle = trEle.querySelector(".uptBtn");
                uptBtnEle.onclick = function (){
                    let uid = this.getAttribute("myid");
                    // console.log(uid);
                    $(".operate .update").fadeIn();
                    $(".cancel").click(()=>{$(".operate .update").css("display","none")});
                    ajax({
                        url:'../../php/admin/upSel.php',
                        method:'post',
                        data:{uid},
                        dataType:'json'
                    }).then(res=>{
                        $("#username").val(res['userName']);
                        $("#password").val(res['passWord']);
                        $("#tel").val(res['tel']);
                        document.getElementById("updBtn").onclick = function(){
                            let pwd = $("#password").val();
                            let tel = $("#tel").val();
                            let reg = /^1[3-9]\d{9}$/g;
                            if( reg.test(tel) ){
                                ajax({
                                    url:'../../php/admin/updUser.php',
                                    method:'post',
                                    dataType:'json',
                                    data:{
                                        uid,
                                        pwd,
                                        tel 
                                    }
                                }).then(res=>{
                                    if( res['status'] == 1 ){
                                        alert("修改成功");
                                        $(".operate .update").css("display","none")
                                        getRenderData(page);
                                    }
                                });
                            } else {
                                alert("手机号不符合规范");
                            }
                        }
                    });
                }
                delBtnEle.onclick = function (){
                    let uid = this.getAttribute("myid");
                    ajax({
                        url:'../../php/admin/delUser.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            uid
                        }
                    }).then(res=>{
                        if( res['status'] == 1 ){
                            alert("删除成功");
                            getRenderData(page);
                        }
                    }); 
                }
                ele.appendChild(trEle);
            });
        }
        function getPageNum (perPage,page){
            ajax({
                url:'../../php/admin/userNum.php',
                dataType:'json',
                method:'post',
                data:{perPage}
            }).then(res=>{
                // console.log(res);
                pageRender(res['totalPage'],page);
            });
        }
        function pageRender (pageNum,page){
            let pagEle = document.querySelector(".pagination");
            pagEle.innerHTML = '' ;
            let str = '' ;
            for(let i = 1 ; i <= pageNum ; i ++ ){
                str += `<a href="javascript:;">${i}</a>`
            }
            pagEle.innerHTML = `<a href="javascript:;" class="prev">⌜</a>
                            ${str}
                            <a href="javascript:;" class="next">⌝</a>`;
            let aEles = pagEle.querySelectorAll("a");
            aEles.forEach(function(aEle,key){
                aEle.onclick = function (){
                    if( aEle.className == "prev" ){
                        if( page > 1 ){
                            page = page - 1 ;
                            getRenderData(page);
                        }
                    } else 
                    if( aEle.className == "next" ){
                        if( page < pageNum ){
                            page = page + 1 ;
                            // console.log(page);
                            getRenderData(page);
                        }
                    } else {
                        page = parseInt(aEle.innerHTML.trim()) ;
                        getRenderData(page);
                    }
                }
                
            });
        }
    }
    if ( opId == 2 ){
        let page = 1 ;  
        let perPage = 5 ; 
        function addProducts(){
            document.querySelector(".addProducts").onclick = function (){
                $("#addGoods").fadeIn();
                $(".cancel").click(()=>{$(".operate #addGoods").css("display","none")});
                ajax({
                    url:'../../php/admin/TypeName.php',
                    method:'post',
                    dataType:'json',
                    data:{
                        opt:'BSelType'
                    }
                }).then(res=>{
                    $("#BType").html(res['data']);
                });
                document.querySelector("#BType").onchange = function (){
                    let TypeVal = this.value;
                    ajax({
                        url:'../../php/admin/TypeName.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'getSubType1',
                            TypeVal
                        }
                    }).then(res=>{
                        if( res['data']){
                            document.querySelector(".subType1").style.display = "inline-block"; 
                            document.querySelector("#subType1").innerHTML = res['data'] ;
                        } else {
                            document.querySelector(".subType1").style.display = "none"; 
                            document.querySelector("#subType1").innerHTML = res['data'] ; 
                        }
                    });
                }
                document.querySelector("#submit6").onclick = function (){
                    let pname = $("#pname").val();
                    let pDescribe = $("#pDescribe").val();
                    let BType = $("#BType").val();
                    let subType1 = $("#subType1").val();
                    let price = $("#price").val();
                    let inventory = $("#inventory").val();
                    let TypeVal ;
                    if(subType1){
                        TypeVal = subType1 ; 
                    } else {
                        TypeVal = BType ; 
                    }
                    let showImg = $("#showImg")[0].files[0];
                    let form = new FormData();
                    form.append("pname",pname); 
                    form.append("pDescribe",pDescribe);
                    form.append("TypeVal",TypeVal);
                    form.append("showImg",showImg); 
                    form.append("price",price);
                    form.append("inventory",inventory);
                    form.append("opt","addGoods");
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        contentType:false,
                        data:form
                    }).then(res=>{
                        if( res['status'] == 1 ){
                            alert("添加成功！");
                            $(".operate #addGoods").css("display","none");
                            getProList(page);
                        } else{
                            alert("操作失败！");
                        }
                    });
                }
            } 
        }
        getProList(page);
        function getProList(page){
            ajax({
                url:'../../php/admin/allType.php',
                method:'post',
                dataType:'json',
                data:{
                    page,
                    perPage,
                    opt:'getProList'
                }
            }).then(res=>{
                // console.log(res);
                let tbodyEle = document.querySelector(".aside > .products table tbody");
                renderPro(tbodyEle,res)
                addProducts();
                getPagePro(page,perPage);
            });
        }  
        function renderPro(ele,dataArr){
            ele.innerHTML = '' ;
            dataArr.forEach((item,key)=>{
                let trEle = document.createElement("tr");
                trEle.innerHTML = `<td>${key+1}</td>
                            <td>${item['pName']}</td>
                            <td><div style="white-space: nowrap;overflow: hidden;text-overflow: ellipsis; width: 200px;">${item['pDescribe']}</div>
                                </td>
                            <td>￥${item['price']}</td>
                            <td>${item['subtypeid']?item['subTypeName']:''}&nbsp;&nbsp;${item['typeName']}</td>
                            <td><img style="width:60px;height: 60px;" src="../../${item['showImg']}" alt=""></td>
                            <td>${item['inventory']}</td>
                            <td><a myid="${item['id']}" class="updPro" href="javascript:;">修改</a></td>
                            <td><a myid="${item['id']}" class="delPro" href="javascript:;">删除</a></td>` ;
                let updProEle = trEle.querySelector(".updPro");
                let delProEle = trEle.querySelector(".delPro");
                updProEle.onclick = function (){
                    $("#updPro").fadeIn();
                    $(".cancel").click(()=>{$(".operate #updPro").css("display","none")});
                    let pid = this.getAttribute("myid");
                    // console.log(pid);
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'updProEle',
                            pid
                        }
                    }).then(res=>{
                        // console.log(res);
                        // console.log(res['data2']);
                        $("#pname1").val(res['pName']);
                        $("#BType1").html(res['data1']);
                        $("#pDescribe1").val(res['pDescribe']);
                        $("#price1").val(res['price']);
                        $("#inventory1").val(res['inventory']);
                        if( res['data2'] ){
                            $(".operate .subType2").css("display","inline-block");
                            $(".operate #subType2").html(res['data2']);
                        } else {
                            $(".operate .subType2").css("display","none");
                            $(".operate #subType2").html(res['data2']);
                        }
                    });
                    document.querySelector("#BType1").onchange = function(){
                        let subTypeId = this.value ; 
                        // console.log(subTypeId);
                        ajax({
                            url:'../../php/admin/TypeName.php',
                            dataType:'json',
                            method:'post',
                            data:{
                                opt:'getSubTypeSel',
                                subTypeId
                            }
                        }).then(res=>{
                            // console.log(res);
                            if( res['data']){
                                $(".operate .subType2").css("display","inline-block");
                                $(".operate #subType2").html(res['data']);
                            } else {
                                $(".operate .subType2").css("display","none");
                                $(".operate #subType2").html(res['data']);
                            }
                        });
                    }
                    document.querySelector("#submit8").onclick = function (){
                        let pname = $("#pname1").val();
                        let BType = $("#BType1").val();
                        let pDescribe = $("#pDescribe1").val();
                        let price = $("#price1").val();
                        let inventory = $("#inventory1").val();
                        let subType = $(".operate #subType2").val();
                        let TypeVal;
                        if( subType ){
                            TypeVal = subType ; 
                        } else {
                            TypeVal = BType ; 
                        }
                        // console.log(pid);
                        ajax({
                            url:'../../php/admin/allType.php',
                            dataType:'json',
                            method:'post',
                            data:{
                                pid,
                                pname,
                                TypeVal,
                                pDescribe,
                                price,
                                inventory,
                                opt:'updatePro'
                            }
                        }).then(res=>{
                            if( res['status'] == 1 ){
                                alert("修改成功！");
                                $(".operate #updPro").css("display","none");
                                getProList(page);
                            } else {
                                alert("操作失败！");
                            }
                        });
                    }
                }
                delProEle.onclick = function (){
                    let pid = this.getAttribute("myid");
                    // console.log(pid);
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'delProEle',
                            pid
                        }
                    }).then(res=>{
                        if( res['status'] == 1 ){
                            alert("删除成功！");
                            getProList(page);
                        } else{
                            alert("操作失败！");
                        }
                    });
                }
                ele.appendChild(trEle);
            });
        }
        function getPagePro(page,perPage){
            ajax({
                url:'../../php/admin/allType.php',
                dataType:'json',
                method:'post',
                data:{
                    opt:'getPagePro',
                    perPage
                }
            }).then(res=>{
                renderPagePro(res['totalPage'],page);
            });
        }
        function renderPagePro(pageNum,page){
            let pagEle = document.querySelector(".aside > .products .pagination");
            pagEle.innerHTML = '' ;
            let str = '' ;
            for(let i = 1 ; i <= pageNum ; i ++ ){
                str += `<a href="javascript:;">${i}</a>`
            }
            pagEle.innerHTML = `<a href="javascript:;" class="prev">⌜</a>
                            ${str}
                            <a href="javascript:;" class="next">⌝</a>`;
            let aEles = pagEle.querySelectorAll("a");
            aEles.forEach(function(aEle,key){
                aEle.onclick = function (){
                    if( aEle.className == "prev" ){
                        if( page > 1 ){
                            page = page - 1 ;
                            getProList(page);
                        }
                    } else 
                    if( aEle.className == "next" ){
                        if( page < pageNum ){
                            page = page + 1 ;
                            // console.log(page);
                            getProList(page);
                        }
                    } else {
                        page = parseInt(aEle.innerHTML.trim()) ;
                        getProList(page);
                    }
                }
                
            });
          
        }
    }
    if ( opId == 4 ){
        let page = 1 ;
        let perPage = 6 ;
        let aEles = document.querySelectorAll(".aside-sec > ul li .transImg");
        let divEles = document.querySelectorAll(".aside-sec > .transImg"); 
        aEles.forEach(function(aEle,key){
            aEle.onclick = function (){
                divEles.forEach(item=>{
                    item.style.display="none";
                });
                if( key == 1 ){
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'getAllImg'
                        }
                    }).then(res=>{
                        let imgArr = res.map(item=>{return "../../"+item['transImg']});
                        let containEle = divEles[key].querySelector(".autoPlayImg");
                        console.log(containEle);
                        new MySwiper(containEle,imgArr);
                    });
                }
                divEles[key].style.display = "block" ;
                aEles.forEach(item=>{item.classList.add("noActive");});
                this.classList.remove("noActive");
            }
        });
        addTransImg();
        function addTransImg(){
            document.querySelector(".addTransImg").onclick = function (){
                $("#addTransImg").fadeIn();
                $(".cancel").click(()=>{$(".operate #addTransImg").css("display","none")});
                document.querySelector("#submit5").onclick = function(){
                    let transImgFile = document.querySelector("#transImg").files[0];
                    // let isTrans = document.getElementsByName("isTrans")[0];
                    // console.dir(isTrans);
                    // let res = $(".isTrans[name=true]").checked().val();
                    let res = $(".isTrans:checked").val();
                    let form = new FormData();
                    form.append("isTrans",res);
                    form.append("transImg",transImgFile);
                    form.append("opt","addTransImg");
                    // console.log(form);
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        contentType:false,
                        dataType:'json',
                        data:form
                    }).then(res=>{
                        // console.log(res);
                        if( res['status'] == 1 ){
                            alert("添加成功！");
                            $(".operate #addTransImg").css("display","none");
                            getImgList(page);
                        } else {
                            alert("添加失败！");
                        }
                    });
                } 
            }
        }
        getImgList(page);
        function getImgList(page){
            ajax({
                url:'../../php/admin/allType.php',
                method:'post',
                dataType:'JSON',
                data:{
                    opt:'getImgList',
                    page,
                    perPage 
                }
            }).then(res=>{
                let divEle = document.querySelectorAll(`.aside > section:nth-of-type(${opId}) > .transImg`)[0];
                let ele = divEle.querySelector("table tbody"); 
                renderImg(ele,res);
                getImgPageNum(page,perPage);
            });
        }
        function renderImg(ele,dataArr){
            ele.innerHTML = '' ;
            dataArr.forEach((item,key)=>{
                let trEle = document.createElement("tr");
                let str = '' ;
                if( item['isTrue'] == 0 ){
                    str=' aside-sec-tab-span-active';
                }
                trEle.innerHTML = `
                    <td>${key+1}</td>
                    <td><img style="width:50px" src="../../${item['transImg']}" alt=""></td>
                    <td><span class="floatl" style="position: relative;top: 50%;transform: translateY(-50%);">是</span><div class="aside-sec-tab-div"  myid="${item['id']}"><a class="aside-sec-tab-span ${str}"></a></div><span class="floatr"  style="position: relative;top: -50%;transform: translateY(50%);">否</span></td>
                    <td><a class="delImg" myid="${item['id']}" href="javascript:;">删除</a></td>` ;
                let isTrueEle = trEle.querySelector(".aside-sec-tab-div");
                let delImgEle = trEle.querySelector(".delImg");
                isTrueEle.onclick = function (){
                    let imgId = this.getAttribute("myid");
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'uptImg',
                            operate:'isTrue',
                            imgId
                        }
                    }).then(res=>{
                        getImgList(page);
                    });
                }
                delImgEle.onclick = function (){
                    let imgId = this.getAttribute("myid");
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'uptImg',
                            operate:'isDelete',
                            imgId
                        }
                    }).then(res=>{
                        if( res['status'] == 1 ){
                            alert("删除成功！");
                            getImgList(page);
                        }
                    });
                }
                ele.appendChild(trEle);
            });
        }
        function getImgPageNum(page,perPage){
            ajax({
                url:'../../php/admin/allType.php',
                method:'post',
                dataType:'json',
                data:{
                    perPage,
                    opt:'getImgPageNum'
                }
            }).then(res=>{
                renderPageImg(res['totalPage'],page)
            });
        }   
        function renderPageImg(pageNum,page){
            let pagEle = document.querySelector(".aside-sec > .transImg > .pagination");
            pagEle.innerHTML = '' ;
            let str = '' ;
            for(let i = 1 ; i <= pageNum ; i ++ ){
                str += `<a href="javascript:;">${i}</a>`
            }
            pagEle.innerHTML = `<a href="javascript:;" class="prev">⌜</a>
                            ${str}
                            <a href="javascript:;" class="next">⌝</a>`;
            let aEles = pagEle.querySelectorAll("a");
            aEles.forEach(function(aEle,key){
                aEle.onclick = function (){
                    if( aEle.className == "prev" ){
                        if( page > 1 ){
                            page = page - 1 ;
                            getImgList(page);
                        }
                    } else 
                    if( aEle.className == "next" ){
                        if( page < pageNum ){
                            page = page + 1 ;
                            // console.log(page);
                            getImgList(page);
                        }
                    } else {
                        page = parseInt(aEle.innerHTML.trim()) ;
                        getImgList(page);
                    }
                }
                
            });
        }
    }
    if ( opId == 5 ){
        let page = 1 ;
        let perPage = 10 ;
        let aEles = document.querySelectorAll(".aside-sec > ul li .type");
        let divEles = document.querySelectorAll(".aside-sec > .type"); 
        aEles.forEach(function(aEle,key){
            aEle.onclick = function (){
                divEles.forEach(item=>{item.style.display="none";});
                divEles[key].style.display = "block" ;
                aEles.forEach(item=>{item.classList.add("noActive");});
                this.classList.remove("noActive");
            }
        });
        getList(page);
        addBigType();
        function addBigType(){
            document.querySelector(".aside-body .addBigType").onclick = function(){
                $("#addBigType").fadeIn();
                $(".cancel").click(()=>{$(".operate #addBigType").css("display","none")});
                document.getElementById("submit").onclick = function(){
                    if($("#TypeName").val()){
                        console.log($("#TypeName").val());
                        ajax({
                            url:"../../php/admin/TypeName.php",
                            method:"post",
                            data:{
                                TypeName:$("#TypeName").val(),
                                opt:"addBigType"
                            },
                            dataType:"json"
                        }).then(res=>{
                            switch(res['status']){
                                case 0:
                                    alert(res['info']);
                                    getList(page);
                                    break;
                                case 1:
                                    alert(res['info']);
                                    getList(page);
                                    break;
                                case 2:
                                    alert(res['info']);
                                    // $("#TypeName").val("")
                                    $("#addBigType").css("display","none");
                                    getList(page);
                                    break;
                                default:'';
                                break;
                            }
                        });
                    } else {
                        alert("类型名称不能为空!");
                    }
                }
            }
        }
        function getList(page){
            ajax({
                url:"../../php/admin/TypeName.php",
                method:"post",
                dataType:"json",
                data:{
                    opt:'select1',
                    page,
                    perPage
                }
            }).then(res=>{
                let uEle = document.querySelector(`.aside > section:nth-of-type(${opId}) .aside-body-table tbody`);
                renderDomBType(uEle,res);
                getPageNum(perPage,page);
            });
        }
        function renderDomBType(ele,dataArr){
            ele.innerHTML = '' ;
            dataArr.forEach((item,key)=>{
                let trEle = document.createElement("tr");
                trEle.innerHTML = `<td>${key+1}</td>
                            <td>${item['typeName']}</td>
                            <td><a class="uptBtn" myid=${item['id']}>修改</a></td>
                            <td><a class="delBtn" myid=${item['id']}>删除</a></td>` ;
                // console.log(item['id']);
                let uptBtnEle = trEle.querySelector(".uptBtn");
                let delBtnEle = trEle.querySelector(".delBtn");
                uptBtnEle.onclick = function(){
                    let typeId = this.getAttribute("myid");
                    $("#updBigType").fadeIn();
                    $(".cancel").click(()=>{$(".operate #updBigType").css("display","none")});
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'selUpt',
                            typeId
                        }
                    }).then(res=>{
                        // console.log(res['typeName']);
                        document.querySelector("#TypeName1").value = res['typeName'] ;
                        let submitEle = document.querySelector("#submit1");
                        submitEle.onclick = function (){
                            let typeName = $("#TypeName1").val();
                            if(typeName){
                                ajax({
                                    url:'../../php/admin/allType.php',
                                    method:'post',
                                    dataType:'json',
                                    data:{
                                        opt:'updBType',
                                        typeName,
                                        typeId
                                    }
                                }).then(res=>{
                                    if( res['status'] == 1 ){
                                        alert("修改成功!");
                                        $(".operate #updBigType").css("display","none");
                                        getList(page);
                                    }
                                });
                            } else {
                                alert("类型名称不能为空!");
                            }
                        }
                    });
                }
                delBtnEle.onclick = function(){
                    let typeId = this.getAttribute("myid");
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'delBType',
                            typeId
                        }
                    }).then(res=>{
                        if( res['status'] == 1 ){
                            alert("删除成功！");
                            getList(page);
                        }
                    });
                }
                ele.appendChild(trEle);
            });
        }
        // pageRender(2,1);
        function getPageNum(perPage,page){
            ajax({
                url:'../../php/admin/allType.php',
                method:'post',
                dataType:'json',
                data:{
                    perPage,
                    typeId:'faType',
                    opt:'BTypePage'
                }
            }).then(res=>{
                pageRender (res['totalPage'],page)
            });
        }
        function pageRender (pageNum,page){
            let pagEle = document.querySelector(`.aside > section:nth-of-type(${opId}) .pagination`);
            pagEle.innerHTML = '' ;
            let str = '' ;
            for(let i = 1 ; i <= pageNum ; i ++ ){
                str += `<a href="javascript:;">${i}</a>`
            }
            pagEle.innerHTML = `<a href="javascript:;" class="prev">⌜</a>
                            ${str}
                            <a href="javascript:;" class="next">⌝</a>`;
            let aEles = pagEle.querySelectorAll("a");
            aEles.forEach(function(aEle,key){
                aEle.onclick = function (){
                    if( aEle.className == "prev" ){
                        if( page > 1 ){
                            page = page - 1 ;
                             getList(page);
                        }
                    } else 
                    if( aEle.className == "next" ){
                        if( page < pageNum ){
                            page = page + 1 ;
                            // console.log(page);
                             getList(page);
                        }
                    } else {
                        page = parseInt(aEle.innerHTML.trim()) ;
                         getList(page);
                    }
                }
                
            });
        }
        // addSType();
        getSTypeList(page);
        function getSTypeList(page){
            // console.log(page,perPage);
            ajax({
                url:'../../php/admin/allType.php',
                method:'post',
                dataType:'json',
                data:{
                    opt:'getSTypeList',
                    page,
                    perPage
                }
            }).then(res=>{
                // console.log(res);
                let tbodyEle = document.querySelector(`.aside > section:nth-of-type(${opId}) > div:nth-of-type(2) tbody`);
                renderDomSType(tbodyEle,res);
                addSType();
                getSPageNum(perPage,page);
            });
        }
        function renderDomSType(ele,dataArr){
            ele.innerHTML = '' ;
            dataArr.forEach((item,key)=>{
                let trEle = document.createElement("tr");
                trEle.innerHTML = `<td>${key+1}</td>
                            <td>${item['subName']}</td>
                            <td>${item['typeName']}</td>
                            <td><a myid="${item['id']}" class = "updStype" href="javascript:;">修改</a></td>
                            <td><a myid="${item['id']}" class = "delStype" href="javascript:;">删除</a</td>
                `;
                let updStypeEle = trEle.querySelector(".updStype");
                let delStypeEle = trEle.querySelector(".delStype");
                updStypeEle.onclick = function (){
                    let typeId = this.getAttribute("myid");
                    // console.log(typeId);
                    $("#updStype").fadeIn();
                    $(".cancel").click(()=>{$(".operate #updStype").css("display","none")});
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'updSelStype',
                            typeId
                        }
                    }).then(res=>{
                        // console.log(res);
                        $("#subTypeName1").html(res['data']);
                        $("#TypeName3").val(res['subTypeName']);
                        document.querySelector("#submit4").onclick = function (){
                            let subTypeId = $("#subTypeName1").val();
                            let TypeName3 = $("#TypeName3").val();
                            ajax({
                                url:'../../php/admin/TypeName.php',
                                method:'post',
                                dataType:'json',
                                data:{
                                    typeId,
                                    subTypeId,
                                    TypeName3,
                                    opt:'updStype'
                                }
                            }).then(res=>{
                                if( res['status'] == 1 ){
                                    alert("修改成功！");
                                    $(".operate #updStype").css("display","none");
                                    getSTypeList(page);
                                } else {
                                    alert("操作失败！");
                                }
                            });
                        }
                    });
                }
                delStypeEle.onclick = function (){
                    let typeId = this.getAttribute("myid");
                    ajax({
                        url:'../../php/admin/allType.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            typeId,
                            opt:'delStype'
                        }
                    }).then(res=>{
                        if( res['status'] == 1 ){
                            alert("删除成功");
                            getSTypeList(page);
                        } else {
                            alert("操作失败");
                        }
                    });
                }
                ele.appendChild(trEle);
            });
        }
        function addSType(){
            document.querySelector(".aside-body .addSType").onclick = function(){
                ajax({
                    url:'../../php/admin/allType.php',
                    method:'post',
                    dataType:'json',
                    data:{
                        opt:'selBType'
                    }
                }).then(res=>{
                    $("#subTypeName").html(res['data']);
                });
                $("#addSType").fadeIn();
                // console.log(111);
                $(".cancel").click(()=>{$(".operate #addSType").css("display","none")});
                document.querySelector("#submit3").onclick = function(){
                    let faTypeId = document.querySelector("#subTypeName").value;
                    let subTypeName = document.querySelector("#TypeName2").value;
                    ajax({
                        url:'../../php/admin/TypeName.php',
                        method:'post',
                        dataType:'json',
                        data:{
                            opt:'addSType',
                            faTypeId,
                            subTypeName
                        }
                    }).then(res=>{
                        if( res['status'] == 1 ){
                            alert("添加成功！");
                            $(".operate #addSType").css("display","none");
                            getSTypeList(page);
                        } else {
                            alert("操作失败！");
                        }
                    });
                }
            }
        }
        
        function getSPageNum(perPage,page){
            ajax({
                url:'../../php/admin/allType.php',
                method:'post',
                dataType:'json',
                data:{
                    perPage,
                    typeId:'subType',
                    opt:'BTypePage'
                }
            }).then(res=>{
                // console.log(res);
                SPageRender (res['totalPage'],page)
            });
        }
        function SPageRender(pageNum,page){
            let pagEle = document.querySelectorAll(`.aside > section:nth-of-type(${opId}) .pagination`)[1];
            pagEle.innerHTML = '' ;
            let str = '' ;
            for(let i = 1 ; i <= pageNum ; i ++ ){
                str += `<a href="javascript:;">${i}</a>`
            }
            pagEle.innerHTML = `<a href="javascript:;" class="prev">⌜</a>
                            ${str}
                            <a href="javascript:;" class="next">⌝</a>`;
            let aEles = pagEle.querySelectorAll("a");
            aEles.forEach(function(aEle,key){
                aEle.onclick = function (){
                    if( aEle.className == "prev" ){
                        if( page > 1 ){
                            page = page - 1 ;
                            getSTypeList(page);
                        }
                    } else 
                    if( aEle.className == "next" ){
                        if( page < pageNum ){
                            page = page + 1 ;
                            // console.log(page);
                            getSTypeList(page);
                        }
                    } else {
                        page = parseInt(aEle.innerHTML.trim()) ;
                        getSTypeList(page);
                    }
                }
                
            });
        }
    }
});